package com.sale.system.dubbo;

import com.sale.common.core.domain.R;
import com.sale.system.api.RemoteSysCustomerService;
import com.sale.system.api.domain.dto.SysCustomerBalance;
import com.sale.system.api.domain.dto.SysCustomerManagementResultDto;
import com.sale.system.domain.dto.SysCustomerManagementDto;
import com.sale.system.service.SysCustomerManagementService;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 客户dubbo实现类
 */
@RequiredArgsConstructor
@Service
@DubboService
public class RemoteSysCustomerServiceImpl implements RemoteSysCustomerService {


    private final SysCustomerManagementService customerManagementService;

    /**
     * 根据客户编码获取客户信息
     *
     * @param customerCode
     * @return
     */
    @Override
    public R<SysCustomerManagementResultDto> getCustomerByCode(String customerCode) {
        return R.ok(customerManagementService.getCustomerByCode(customerCode));
    }

    /**
     * 获取启用的客户列表
     * @return
     */
    @Override
    public Map<String, String> getOpenCustomerFinance() {
        List<SysCustomerManagementDto> openCustomer = customerManagementService.getOpenCustomer();
        Map<String, String> collect = openCustomer.stream()
            .collect(Collectors.toMap(SysCustomerManagementDto::getCustomerNum, SysCustomerManagementDto::getCustomerName));
        return collect;
    }

    /**
     * 通过客户名称获取客户编码(财务模块使用)
     * @param customerName
     * @return
     */
    @Override
    public String getCodeByCustomerName(String customerName) {
        return customerManagementService.getCodeByCustomerName(customerName);
    }

    /**
     * 通过客户编码更新余额
     */
    @Override
    public boolean updateClientBalance(SysCustomerBalance customerBalance) {
        return customerManagementService.updateClientBalance(customerBalance);
    }
}
